import Vue from 'vue';
import VueRouter from 'vue-router';
import LoginRouter from './login';
import LayoutPage from '@/layout';

Vue.use(VueRouter);

// 公共路由
export const constanRoutes = [
  {
    path: '/',
    component: LayoutPage,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'dashboard',
        meta: { title: '首页' },
        component: () => import('@/views/dashboard'),
      },
    ],
    hidden: false,
  },
  {
    path: '/form',
    component: LayoutPage,
    meta: { title: '组件' },
    redirect: '/form/form',
    children: [
      {
        name: 'Form',
        path: 'form',
        meta: { title: 'form' },
        component: () => import('@/views/comp/form'),
      },
    ],
  },
  ...LoginRouter,
];
/**
 * 根据用户动态实现路由
 */
export const asyncRoutes = [];
// 防止重复路由跳转报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

const router = new VueRouter({
  base: "web",
  mode: 'history', // 去掉url中的#
  scrollBehavior: () => ({ y: 0 }),
  routes: constanRoutes,
});
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}
export default router;
